package com.hlf.modules.merchant.coupon.service.impl;

import com.hlf.common.constants.Constants;
import com.hlf.common.exception.BusinessException;
import com.hlf.common.utils.IdWorkerFactory;
import com.hlf.modules.h5.interceptor.AuthorizationInterceptor;
import com.hlf.modules.merchant.coupon.entity.Coupon;
import com.hlf.modules.merchant.coupon.mapper.CouponMapper;
import com.hlf.modules.merchant.coupon.service.CouponService;
import com.hlf.modules.merchant.store.entity.Store;
import com.hlf.modules.merchant.store.mapper.StoreMapper;
import com.hlf.modules.sys.entity.SysUserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 优惠券服务层
 * Created by Administrator on 2017/10/13.
 */
@Service("CouponService")
public class CouponServiceImpl implements CouponService {

    @Autowired
    private CouponMapper couponMapper;
    @Autowired
    private StoreMapper storeMapper;

    /**
     * 优惠券列表
     *
     * @param storeId
     * @return
     */
    @Override
    public List<Coupon> list(String storeId) {
        return couponMapper.queryListByStoreId(Long.parseLong(storeId));
    }

    /**
     * 创建优惠券
     *
     * @param couponType
     * @param couponNum
     * @param spendMin
     */
    @Override
    public void create(SysUserEntity userEntity, String couponType, String couponNum, String spendMin) {
        Long userId = userEntity.getUserId();
        int couponInt = Integer.parseInt(couponNum);
        Coupon coupon = new Coupon();
        Byte couponTypeB = Byte.parseByte(couponType);
        coupon.setId(IdWorkerFactory.nextId());
        coupon.setStoreId(Long.parseLong(userEntity.getCurrentStoreId()));
        coupon.setCouponType(couponTypeB);
        coupon.setSpendMin(Integer.parseInt(spendMin));
        if (couponInt <= 0) {
            throw new BusinessException("满减金额输入有误!");
        }
        if (Constants.STORE_COUPON_TYPE_REBATE.equals(couponTypeB)) {
            if (couponInt > 10) {
                throw new BusinessException("折扣不能超过100%!");
            }
            coupon.setCouponTypeName("整单折扣");
        } else {
            coupon.setCouponTypeName("整单减免");
        }
        coupon.setCouponNum(couponInt);
        coupon.setLastUpdateBy(userId);
        couponMapper.insertSelective(coupon);
    }

    /**
     * 打开关闭优惠券使用开关
     *
     * @param
     */
    @Override
    public void open(SysUserEntity user, String storeCouponStatusStr) {
        Long storeId = Long.parseLong(user.getCurrentStoreId());
        Store store = storeMapper.queryObject(storeId);

        Long userId = user.getUserId();
        store.setLastUpdateBy(userId);
        Byte storeCouponStatus = Byte.parseByte(storeCouponStatusStr);
        store.setStoreCouponStatus(storeCouponStatus);
        int i = storeMapper.updateByPrimaryKeySelective(store);
        if (i == 0) {
            throw new BusinessException("打开或关闭失败!");
        }
    }


    /**
     * 删除优惠券
     */
    @Override
    public void delete(String id, HttpServletRequest request) {
        Long userId = (Long) request.getAttribute(AuthorizationInterceptor.LOGIN_USER_KEY);
        Coupon coupon = new Coupon();
        coupon.setId(Long.parseLong(id));
        coupon.setLastUpdateBy(userId);
        coupon.setDelFlag(String.valueOf(Constants.DELETE_FLAG_UNABLE));
        couponMapper.updateByPrimaryKeySelective(coupon);
    }
}
